package com.android.tiango.baba.act;

import java.util.ArrayList;

import uk.co.jasonfry.android.tools.ui.SwipeView;
import uk.co.jasonfry.android.tools.ui.SwipeView.OnPageChangedListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.cookie.welcome.WelcomeAnimEntity_0;
import com.android.tiango.baba.cookie.welcome.WelcomeAnimEntity_1;
import com.android.tiango.baba.cookie.welcome.WelcomeAnimEntity_2;
import com.android.tiango.baba.cookie.welcome.WelcomeAnimEntity_3;

public class ActTianGoBaWelcome extends BaBaseActivity {
	SwipeView mSwipeView;
	ArrayList<Object> lists = new ArrayList<Object>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_welcome3);
		mSwipeView = (SwipeView) findViewById(R.id.swipe_view);

		int margin = mSwipeView.setPageWidth(getResources().getDisplayMetrics().widthPixels);

		loadImages();

		for (int i = 0; i < 4; i++) {
			FrameLayout frame = new FrameLayout(this);
			if (i == 0) {
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
						LinearLayout.LayoutParams.FILL_PARENT);
				params.leftMargin = margin;
				frame.setLayoutParams(params);
			} else if (i == 6) {
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
						LinearLayout.LayoutParams.FILL_PARENT);
				params.rightMargin = margin;
				frame.setLayoutParams(params);
			}

			mSwipeView.addView(frame);
		}

		((ViewGroup) mSwipeView.getChildContainer().getChildAt(0))
				.addView(((WelcomeAnimEntity_0) lists.get(0)).getView());
		((ViewGroup) mSwipeView.getChildContainer().getChildAt(1))
				.addView(((WelcomeAnimEntity_1) lists.get(1)).getView());
		SwipeImageLoader mSwipeImageLoader = new SwipeImageLoader();

		mSwipeView.setOnPageChangedListener(mSwipeImageLoader);

		// mSwipeView.setPageControl(mPageControl);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	private class SwipeImageLoader implements OnPageChangedListener {

		public void onPageChanged(int oldPage, int newPage) {
			if (newPage > oldPage) {
				if (newPage != (mSwipeView.getPageCount() - 1)) {
					if (lists.get(newPage + 1) instanceof WelcomeAnimEntity_1) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage + 1))
								.addView(((WelcomeAnimEntity_1) lists.get(newPage + 1)).getView());
					} else if (lists.get(newPage + 1) instanceof WelcomeAnimEntity_2) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage + 1))
								.addView(((WelcomeAnimEntity_2) lists.get(newPage + 1)).getView());

					} else if (lists.get(newPage + 1) instanceof WelcomeAnimEntity_3) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage + 1))
								.addView(((WelcomeAnimEntity_3) lists.get(newPage + 1)).getView());

					} else if (lists.get(newPage + 1) instanceof WelcomeAnimEntity_0) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage + 1))
								.addView(((WelcomeAnimEntity_0) lists.get(newPage + 1)).getView());

					}

				}
				if (oldPage != 0) {
					((ViewGroup) mSwipeView.getChildContainer().getChildAt(oldPage - 1))
							.removeAllViews();
					if ((newPage + 1) == mSwipeView.getPageCount()) {

					}

				}
				if (lists.get(newPage) instanceof WelcomeAnimEntity_1) {
					((WelcomeAnimEntity_1) lists.get(newPage)).startAnim();
				} else if (lists.get(newPage) instanceof WelcomeAnimEntity_2) {
					((WelcomeAnimEntity_2) lists.get(newPage)).startAnim();
				} else if (lists.get(newPage) instanceof WelcomeAnimEntity_3) {
					((WelcomeAnimEntity_3) lists.get(newPage)).startAnim();

				} else if (lists.get(newPage) instanceof WelcomeAnimEntity_0) {
					((WelcomeAnimEntity_0) lists.get(newPage)).startAnim();

				}
			} else {
				if (newPage != 0) {
					if (lists.get(newPage - 1) instanceof WelcomeAnimEntity_1) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage - 1))
								.addView(((WelcomeAnimEntity_1) lists.get(newPage - 1)).getView());
					} else if (lists.get(newPage - 1) instanceof WelcomeAnimEntity_2) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage - 1))
								.addView(((WelcomeAnimEntity_2) lists.get(newPage - 1)).getView());

					} else if (lists.get(newPage - 1) instanceof WelcomeAnimEntity_3) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage - 1))
								.addView(((WelcomeAnimEntity_3) lists.get(newPage - 1)).getView());

					} else if (lists.get(newPage - 1) instanceof WelcomeAnimEntity_0) {
						((ViewGroup) mSwipeView.getChildContainer().getChildAt(newPage - 1))
								.addView(((WelcomeAnimEntity_0) lists.get(newPage - 1)).getView());

					}
				}
				if (oldPage != (mSwipeView.getPageCount() - 1)) {
					((ViewGroup) mSwipeView.getChildContainer().getChildAt(oldPage + 1))
							.removeAllViews();
				}
				if (lists.get(newPage) instanceof WelcomeAnimEntity_1) {
					((WelcomeAnimEntity_1) lists.get(newPage)).startReAnim();
				} else if (lists.get(newPage) instanceof WelcomeAnimEntity_2) {
					((WelcomeAnimEntity_2) lists.get(newPage)).startReAnim();
				} else if (lists.get(newPage) instanceof WelcomeAnimEntity_3) {
					((WelcomeAnimEntity_3) lists.get(newPage)).startReAnim();
				} else if (lists.get(newPage) instanceof WelcomeAnimEntity_0) {
					((WelcomeAnimEntity_0) lists.get(newPage)).startReAnim();
				}
			}

		}
	}
	
	private class LoginListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			
		}
	}
	private class StartListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			
		}
	}

	private void loadImages() {
		WelcomeAnimEntity_0 entity_0 = new WelcomeAnimEntity_0(ActTianGoBaWelcome.this);
		WelcomeAnimEntity_1 entity_1 = new WelcomeAnimEntity_1(ActTianGoBaWelcome.this);
		WelcomeAnimEntity_2 entity_2 = new WelcomeAnimEntity_2(ActTianGoBaWelcome.this);
		WelcomeAnimEntity_3 entity_3 = new WelcomeAnimEntity_3(ActTianGoBaWelcome.this);
		lists.add(entity_0);
		lists.add(entity_1);
		lists.add(entity_2);
		lists.add(entity_3);

	}
}